using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;

using Affirma.ThreeSharp;
using Affirma.ThreeSharp.Query;
using Affirma.ThreeSharp.Model;
using Affirma.ThreeSharp.Statistics;

namespace Affirma.CloudFront.AdminTool
{
    public partial class frmCreateBucket : Form
    {
        public frmCreateBucket()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (AccountManager.Instance.IsServiceSelected(true))
                {
                    using (BucketAddRequest request = new BucketAddRequest(this.txtBucketName.Text.ToLower().Trim().Replace(' ', '_')))
                    {
                        if (this.ddlLocation.Text != "US")
                        {
                            StringBuilder sbBucketConfig = new StringBuilder();
                            sbBucketConfig.AppendLine("<CreateBucketConfiguration>");
                            sbBucketConfig.AppendLine("<LocationConstraint>" + this.ddlLocation.Text + "</LocationConstraint>");
                            sbBucketConfig.AppendLine("</CreateBucketConfiguration>");
                            request.LoadStreamWithString(sbBucketConfig.ToString());
                        }

                        using (BucketAddResponse response = AccountManager.Instance.Service.BucketAdd(request))
                        { }

                        this.DialogResult = DialogResult.OK;

                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                this.DialogResult = DialogResult.None;
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}